package core;

import java.util.ArrayList;
import java.util.List;

public class CalculateTools {
	static List<PriceData> calMovingAverage(List<PriceData> prices, int interval) {
		if (interval >= prices.size()) {
			System.err.println("interval is shorter than prices length!");
			return null;
		}

		List<PriceData> results = new ArrayList<PriceData>(prices.size());
		PriceData sum = new PriceData();
		for (int i = 0; i < interval; ++i) {
			results.add(prices.get(i));
			sum = PriceData.add(sum, prices.get(i));
		}

		for (int i = interval; i < prices.size(); ++i) {
			sum = PriceData.sub(sum, prices.get(i - interval));
			sum = PriceData.add(sum, prices.get(i));
			results.add(PriceData.div(sum, interval));
		}

		return results;
	}
}
